package com.production.CoursePlatform.controller;

import com.production.CoursePlatform.entity.Discussion;
import com.production.CoursePlatform.entity.Reply;
import com.production.CoursePlatform.service.ReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/replies")
public class ReplyController {

    @Autowired
    private ReplyService replyService;

    // 创建新的回复记录
    @PostMapping("/create")
    public ResponseEntity<String> createReply(@RequestBody Reply reply) {
        replyService.createReply(reply);
        return ResponseEntity.ok("Reply created successfully!");
    }

    // 根据r_id查找回复记录
    @GetMapping("/getBYrID/{rID}")
    public ResponseEntity<Reply> getReplyByRId(@PathVariable int rID) {
        Reply reply = replyService.findReplyByRId(rID);
        if (reply != null) {
            return ResponseEntity.ok(reply);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

    //获取帖子通过cmt_id
    @GetMapping("/getByCmtID/{cmtID}")
    public ResponseEntity<List<Reply>> getByCtID(@PathVariable int cmtID) {
        List<Reply> replies = replyService.findReplyByCmtId(cmtID);
        return ResponseEntity.ok(replies);
    }

    // 删除指定r_id的回复记录
    @DeleteMapping("/delete/{rID}")
    public ResponseEntity<String> deleteReply(@PathVariable int rID) {
        int isDeleted = replyService.deleteReply(rID);
        if (isDeleted>0) {
            return ResponseEntity.ok("Reply deleted successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Reply not found");
        }
    }

    // 更新回复记录
    @PutMapping("/update")
    public ResponseEntity<String> updateReply(@RequestBody Reply reply) {
        boolean isUpdated = replyService.updateReply(reply);
        if (isUpdated) {
            return ResponseEntity.ok("Reply updated successfully!");
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Reply not found");
        }
    }
}

